// @Author EthanScriptOn
// @Desc
package result_parser

import (
	"encoding/json"
	"fmt"
	"gitee.com/fatzeng/srf_switch_stream_bridge/bridge"
	"gitee.com/fatzeng/srf_switch_stream_bridge/framework/actuator"
	"reflect"
)

type BytesParser struct {
}

func (b *BytesParser) Parse(ctx *actuator.SrfSwitchBridgeActuatorContext, config *bridge.BridgeConfig, response interface{}) (interface{}, error) {
	if config.ResultType == nil {
		return nil, fmt.Errorf("typ is nil")
	}
	if response == nil {
		return nil, fmt.Errorf("response is nil")
	}
	bytes, ok := response.([]byte)
	if !ok {
		return nil, fmt.Errorf("response is not []byte")
	}
	resValue := reflect.New(config.ResultType)
	instance := resValue.Interface()
	err := json.Unmarshal(bytes, instance)
	if err != nil {
		return nil, err
	}
	return instance, nil
}
